﻿/**************************
 * EVENT HANDLER
 */

function OnLoad() {
	LoadRss('');
}

function OnFrmToggle() {
	if ($('#newrss .content').css('display') == 'none') {
		// show new rss form
		$('#newrss .content').show();
		$('#newrss .buttons img').attr('src', 'image/collapse.png');
	} else {
		// hide form
		$('#newrss .content').hide();
		$('#newrss .buttons img').attr('src', 'image/expand.png');
	}
}

function OnStyle(id)
{
	var theme = 'style/' + id + '.css';
	$('#style').attr('href', theme);
	$('.themes li').removeClass('current');
	$('#'+id).addClass('current');
}

function OnKey(event) {
	if (event.keyCode == 13) OnSearch();
}

function OnSearch() {
	LoadRss($('#keyWord').val());
}

function LoadRss(keyword) {
	var callBack = function (result) {
		if (result) {
			var html = '';
			for(var i = 0; i < result.length; i++) {
				var item = result[i];
				html += getRssView(item);
			}
			$('#content').html(html);	// append rows to table
		}
	};
	$('#content').html('<img src="image/loading.gif" alt="" />');	// show loading animation
	$('#content').show();
	ajax('search', keyword, callBack);
}

function OnAdd()
{
	// add new rss
	var obj = new Object();
	obj.name = $('#name').val();
	obj.url = $('#url').val();

	if (obj.name == "")	{
		alert('Chưa nhập tên.');
		return;
	}
	
	if (obj.url == "") {
		alert('Chưa nhập url.');
		return;
	}
	
	var data = JSON.stringify(obj);
	var callBack = function (result) {
		if (result > 0) {
			obj.id = result;
			var html = getRssView(obj);
			$('#content').append(html);	// add item to page
			// reset form
			$('#name').val('');
			$('#url').val('');
		}
	};
	ajax('create', data, callBack);
}

function OnDel(id)
{
	var accept = confirm("Bạn có muốn xóa mục này?");
	if (!accept) return;
	var callBack = function (result) {
		if (result > 0) {
			$('#item' + id).remove();	// remove item from page
		}
	};
	ajax('delete', id, callBack);
}

function OnShowUpd(id) {
	// show update form
	var callBack = function (result) {
		if (result) {
			$('#item'+id).children('.content').html($('#updFrm').html()).show();
			$('#item'+id+' input[name="name"]').val(result.name);
			$('#item'+id+' input[name="url"]').val(result.url);
			$('#item'+id+' button[name="save"]').click(id, function (event) { OnUpd(event.data); });
		}
	};
	ajax('read', id, callBack);
}

function OnUpd(id)
{
	var obj = new Object();
	obj.id = id;
	obj.name = $('#item'+id+' input[name="name"]').val();
	obj.url = $('#item'+id+' input[name="url"]').val();

	if (obj.name == "")	{
		alert('Chưa nhập tên.');
		return;
	}
	
	if (obj.url == "") {
		alert('Chưa nhập url.');
		return;
	}
	
	var data = JSON.stringify(obj);
	var callBack = function (result) {
		if (result > 0) {
			var html = getRssView(obj);
			// update view
			$('#item'+id+' h2 a').attr('href', obj.url);
			$('#item'+id+' h2 a').text(obj.name);
		}
		$('#item'+id+' .content').hide();
	};
	ajax('update', data, callBack);
}

function OnExplore(id) {
	// collapse
	if ($('#item'+id+' .content').css('display') != 'none') {
		$('#item'+id+' .content').hide();
		$('#item'+id+' img[name="explore"]').attr('src', 'image/expand.png');
		return;
	}
	
	// expand
	var url = $('#item'+id+' h2 a').attr('href');
	var callBack = function (result) {
		if (result) {
			var html = '';
			for(var i = 0; i < result.length; i++) {
				var item = result[i];
				html += getRssItemView(item);
			}
			$('#item'+id+' .content').html(html);
		}
	};
	ajax('explore', url, callBack);
	$('#item'+id+' .content').show();
	$('#item'+id+' .content').html('<img src="image/loading.gif" alt="loading..."/>')
	$('#item'+id+' img[name="explore"]').attr('src', 'image/collapse.png');
}

/**************************
 * VIEW
 */

function getRssView(item) {
	var html = 
		'<div class="box" id="item%s">' +
			'<div class="heading">' +
				'<h2><a href="%s" target="_blank" >%s</a></h2>' +
				'<div class="buttons">' +
					'<a href="javascript:OnShowUpd(%s)"><img src="image/edit.png" alt="edit"/></a>' +
					'<a href="javascript:OnDel(%s)"><img src="image/delete.png" alt="delete"/></a>' +
					'<a href="javascript:OnExplore(%s)"><img name="explore" src="image/expand.png" alt="expand"/></a>' +
				'</div>' +
			'</div>' +
			'<div class="content"></div>' +
		'</div>';
	html = sprintf(html, item.id, item.url, item.name, item.id, item.id, item.id);
	return html;
}

function getRssItemView(item) {
	var html =
		'<div class="rssitem">'+
			'<h3><a href="%s" target="_blank">%s</a></h3>' +
			'<p>%s</p>'+
		'</div>';
	html = sprintf(html, item.link, item.title, item.description);
	return html;
}

/**************************
 * COMMON
 */

//send async ajax post
function ajax(method, data, callback) {
	$.ajax({
		url: 'ajax.php',
		type: "POST",
		data: 'method=' + method + '&data=' + data,
		success: callback
	});
}

